package com.ggb.reactive.controller.admin

import com.ggb.reactive.constant.*
import com.ggb.reactive.domain.vo.BusinessDataVO
import com.ggb.reactive.domain.vo.DishOverViewVO
import com.ggb.reactive.domain.vo.OrderOverViewVO
import com.ggb.reactive.domain.vo.SetmealOverViewVO
import com.ggb.reactive.model.Result
import com.ggb.reactive.service.DishService
import com.ggb.reactive.service.OrderService
import com.ggb.reactive.service.SetmealService
import com.ggb.reactive.service.UserService
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import lombok.RequiredArgsConstructor
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import reactor.core.publisher.Mono
import java.time.LocalDateTime
import java.time.LocalTime

@RestController
@RequestMapping("/admin/workspace")
@RequiredArgsConstructor
@Tag(name = "工作台相关接口")
class WorkSpaceController(
    val orderService: OrderService,
    val userService: UserService,
    val dishService: DishService,
    val setmealService: SetmealService
) {

    @Operation(summary = "工作台今日数据查询")
    @GetMapping("/businessData")
    fun businessData(): Mono<Result<BusinessDataVO>> {
        val begin = LocalDateTime.now().with(LocalTime.MIN)
        val end = LocalDateTime.now().with(LocalTime.MAX)

        val orderCountMono = orderService.countByMap(null, begin, end)
        val turnoverMono = orderService.sumByMap(null, begin, end)
        val validOrderCountMono = orderService.countByMap(COMPLETED, begin, end)
        val orderCompletionRateMono = orderService.countByMap(COMPLETED, begin, end)
        val userCountMono = userService.countByMap(begin, end)

        return Mono.zip(
            orderCountMono,
            turnoverMono,
            validOrderCountMono,
            orderCompletionRateMono,
            userCountMono
        ).map { tuple ->
            val orderCount = tuple.t1
            val turnover = tuple.t2
            val validOrderCount = tuple.t3
            val orderCompletionRate = if (orderCount != 0) {
                tuple.t4.toDouble() / orderCount.toDouble()
            } else {
                0.0
            }
            val userCount = tuple.t5
            val unitPrice =   if (validOrderCount != 0) {
                turnover / validOrderCount.toDouble()
            } else {
                0.0
            }
            Result.success(
                BusinessDataVO(
                    turnover = turnover,
                    validOrderCount = validOrderCount,
                    orderCompletionRate = orderCompletionRate,
                    unitPrice = unitPrice,
                    newUsers = userCount
                )
            )
        }
    }


    @Operation(summary = "查询订单管理数据")
    @GetMapping("/overviewOrders")
    fun overviewOrder(): Mono<Result<OrderOverViewVO>> {
        val begin = LocalDateTime.now().with(LocalTime.MIN)
        //待接单
        val waitingOrders = orderService.countByMap(TO_BE_CONFIRMED, begin, null)

        // 待派送
        val deliveredOrders = orderService.countByMap(CONFIRMED, begin, null)

        // 已完成
        val completedOrders = orderService.countByMap(COMPLETED, begin, null)

        // 已取消
        val cancelledOrders = orderService.countByMap(COMPLETED, begin, null)

        // 已取消
        val allOrders = orderService.countByMap(null, begin, null)

        return Mono.zip(
            waitingOrders,
            deliveredOrders,
            completedOrders,
            cancelledOrders,
            allOrders
        ).map { tuple ->
            val waiting = tuple.t1
            val delivered = tuple.t2
            val completed = tuple.t3
            val cancelled = tuple.t4
            val all = tuple.t5
            Result.success(
                OrderOverViewVO(
                    waiting,
                    delivered,
                    completed,
                    cancelled,
                    all
                )
            )
        }
    }


    @Operation(summary = "查询菜品总览")
    @GetMapping("/overviewDishes")
    fun dishOverView(): Mono<Result<DishOverViewVO>> {
        val enableCount =  dishService.countByMap(ENABLE,null)
        val disableCount =  dishService.countByMap(DISABLE,null)
        return Mono.zip(
            enableCount,
            disableCount
        ).map { tuple ->
            val enable = tuple.t1
            val disable = tuple.t2
            Result.success(
                DishOverViewVO(
                    enable,
                    disable
                )
            )
        }
    }

    @Operation(summary = "查询套餐总览")
    @GetMapping("/overviewSetmeals")
    fun setmealOverView(): Mono<Result<SetmealOverViewVO>> {
        val enableCount =  setmealService.countByMap(ENABLE,null)
        val disableCount =  setmealService.countByMap(DISABLE,null)
        return  Mono.zip(
            enableCount,
            disableCount
        ).map { tuple ->
            val enable = tuple.t1
            val disable = tuple.t2
            Result.success(
                SetmealOverViewVO(
                    enable,
                    disable
                )
            )
        }
    }

}